﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace BusinessLogicLayer
{
    public class OrderFacade
    {
        InventorySoreDataContext context;
        OrderDetails orderdetails;
        Orders odr;
        public OrderFacade()
        {
            context = new InventorySoreDataContext();
        }
        public IQueryable FindAll()
        {
            var orders = from ord in context.Orders select ord;
            return orders;
        }
        public Orders FindOrderByID(string PoNo)
        {
            var order = (from ord in context.Orders where ord.PONo == PoNo select ord).Single();
            return order;
        }

        public IQueryable FindAllOrderBySupplier(string supID)
        {
            var poNumbers = (from ord in context.Orders
                             where ord.SupplierID == supID
                             select ord);
            return poNumbers;
        }

        public IQueryable FindPendingBySupplier(string supID)
        {
            var poNumbers = (from ord in context.Orders
                             where ord.Status == "Pending"
                             && ord.SupplierID == supID
                             select ord);
            return poNumbers;
        }
        public IQueryable FindOrderDetailsByID(string PONo)
        {
            var orderdetails = (from ord in context.OrderDetails where ord.PONo == PONo select  new 
            {
                ItemCode=ord.ItemCode,
                Description=ord.Item.Description,
                OrderedQty= ord.QtyOrdered,
                ReceivedQty=ord.QtyReceived,
                Remarks=ord.Remark
            });
            return orderdetails;
        }
        public OrderDetails FindOrderDetailsByIDAndCode(string PONo,string itemCode)
        {
            var orderdetails = (from ord in context.OrderDetails
                                where ord.PONo == PONo && ord.ItemCode==itemCode
                                select ord).Single();
            return orderdetails;
        }
        public void UpdateOrderDetails(string PONo, string itemCode,int qtyReceived, string remarks)
        {
            orderdetails = FindOrderDetailsByIDAndCode(PONo, itemCode);
            orderdetails.QtyReceived = qtyReceived;
            orderdetails.Remark = remarks;
            context.SubmitChanges();
        }

        public void UpdateOrderStatus(string PONo)
        {
            odr = FindOrderByID(PONo);
            odr.Status = "Purchased";
            context.SubmitChanges();
        }
        public IQueryable FindforSendOrder(string PONo)
        {
            var sendOrder = (from ordt in context.OrderDetails
                             join tdl in context.TenderLists on ordt.ItemCode equals tdl.ItemCode
                             where ordt.PONo == PONo
                             select new
                             {
                                 ItemCode = ordt.ItemCode,
                                 QtyOrdered = ordt.QtyOrdered,
                                 Price = tdl.Price,
                                 Amount = (ordt.QtyOrdered) * (tdl.Price),
                             });
            return sendOrder;
        }

        public string GetOrderID(string supplierID,string  clerkID,DateTime expectedDate)
        {
            string listID = null;
            if (Convert.ToInt32(DateTime.Now.DayOfWeek) == 5)
            {
                int query = (from ord in context.Orders
                             select ord).Count();
                query++;
                listID = "700000" + query;
                Orders ords = new Orders();
                ords.SupplierID = supplierID;
                ords.PONo = listID;
                ords.ClerkID = clerkID;
                ords.ExpectedDate = expectedDate;
                ords.OrderDate = DateTime.Now.Date;
                ords.Status = "Pending";
                context.Orders.InsertOnSubmit(ords);
                context.SubmitChanges();
            }

            else
            {
                try
                {
                    var query = (from ord in context.Orders
                                 where ord.SupplierID == supplierID
                                 orderby ord.PONo descending
                                 select ord);
                    if (query.Count() > 1)
                    {
                        listID = query.First().PONo;
                    }
                    else
                    listID = query.Single().PONo;
                }
                catch(Exception ee)
                {
                    int query = (from ord in context.Orders
                                 select ord).Count();
                    query++;
                    listID = "700000" + query;
                    Orders ords = new Orders();
                    ords.SupplierID = supplierID;
                    ords.PONo = listID;
                    ords.ClerkID = clerkID;
                    ords.ExpectedDate = expectedDate;
                    ords.DelieveredDate = DateTime.Now.Date;
                    ords.OrderDate = DateTime.Now.Date;
                    ords.Status = "Pending";
                    context.Orders.InsertOnSubmit(ords);
                    context.SubmitChanges();
                    return listID;
                }
            }
            return listID;
        }
        public void insertOrderDetails(List<OrderDetails> orDetails)
        {
            context.OrderDetails.InsertAllOnSubmit(orDetails);
            context.SubmitChanges();
        }

        public void UpdateOrderQty(string PONo, string itemCode,int qtyOrdered)
        {
            orderdetails = FindOrderDetailsByIDAndCode(PONo, itemCode);
            orderdetails.QtyOrdered += qtyOrdered;
            context.SubmitChanges();
        }

        public Boolean ValidateExist(string PONo, string itemCode)
        {
            int query = (from ord in context.OrderDetails where ord.PONo==PONo && ord.ItemCode==itemCode
                         select ord).Count();
            if (query >= 1)
            {
                return true;
            }
            else
                return false;
        }
        //public void insertPurchaseOrder(Orders order, string supplierID, OrderDetails[] orderdetails)
        //{
        //    order.OrderDate = DateTime.Now.Date;
        //    order.Status = "Pending";

        //    context.Orders.InsertOnSubmit(order);
        //    context.OrderDetails.InsertAllOnSubmit(orderdetails);
        //    context.SubmitChanges();
        //}

        //public IQueryable FindforSendOrder(string PONo, string supID)
        //{
        //    var sendOrder = (from ordt in context.OrderDetails
        //                  join tdl in context.TenderLists on ordt.ItemCode equals tdl.ItemCode
        //                  where ordt.PONo == PONo && tdl.SupplierID==supID
        //                  select new
        //                  {
        //                      ordt.ItemCode,
        //                      ordt.QtyOrdered,
        //                      tdl.Price,
        //                      amount=(ordt.QtyOrdered)*(tdl.Price),
        //                  });
        //    return sendOrder;
        //}

      
        
    }
}
